/*
 * MessageDaoImpl.java
 *
 * Created on December 31, 2008, 9:25 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.vacuela.publishmx.dal;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.vacuela.publishmx.domains.ContactNumber;
import org.vacuela.publishmx.domains.Message;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author nestor.carrasco
 */
public class MessageDaoImpl extends SqlMapClientDaoSupport  implements MessageDaoIfc{
    @Transactional
    public boolean saveUpdate(Message msg) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        if (msg.getId() == null){
            template.insert("insertMessage", msg);
        }
        return true;
    }
    
    @Transactional
    public boolean scheduleIndMessages(Message msg){
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        String[] indPhoneNumbers = msg.getIndPhoneNumbers();
        if ( indPhoneNumbers != null){
            Map mapParams = new HashMap();
            mapParams.put("userId",msg.getUserId());
            mapParams.put("msgId",msg.getId());
            mapParams.put("priority",msg.getPriority());
            for (int i = 0; i < indPhoneNumbers.length; i++){
                mapParams.put("phoneNumber",indPhoneNumbers[i]);
                template.insert("insertIndMessageScheduled",mapParams);
            }
        }
        return true;
    }
    
    @Transactional
    public boolean scheduleMassiveMessages(Message msg){
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        String[] contactIdsLst = msg.getContactIds();
        if ( contactIdsLst != null){
            List  lstValue = Arrays.asList(contactIdsLst);
            Map<Integer, Integer> contactNumberMap =
                    ( Map<Integer, Integer>) template.queryForMap("getNumberFromContactSelected",lstValue,"id","contactId");
            
            Map mapParams = new HashMap();
            mapParams.put("userId",msg.getUserId());
            mapParams.put("msgId",msg.getId());
            mapParams.put("priority",msg.getPriority());
            for  (Integer key: contactNumberMap.keySet()){
                mapParams.put("numberId",key);
                mapParams.put("contactId",contactNumberMap.get(key));
                //  System.out.println("Key: " +  key + " Value: " + contactNumberMap.get(key) );
                template.insert("insertMassiveMessageScheduled",mapParams);
            }
        }
        return true;
    }
    public List getMessageByUserId(Integer userId) { return null;
    }
    
    public List getMessageList() { return null;
    }
    
    public Message getMessage(Integer msgId) {
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Message msg = (Message)template.queryForObject("getMsgById",msgId);
        return msg;
    }
    
    public List getMessagesSentBetween(String start, String finish) {
        return null;
    }
    
    public  List<Message> getNewInputMessages(){
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        List<Message> lstMsg = ( List<Message>)template.queryForList("readNewInputMessages", null);
        return lstMsg;
    }
    
    public boolean changeStatus(String msgId, String newStatus){
        SqlMapClientTemplate template = getSqlMapClientTemplate();
        Map mapParam = new HashMap();
        mapParam.put("msgId",msgId);
        mapParam.put("newStatus",newStatus);
        mapParam.put("result", new Integer(-1));
        template.queryForObject("changeStatus",mapParam);
        return true;
    }
    
}
